﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace ZJ.Finance.BusinessRule
{

    public static class StringExpand
    {
        public static string SqlFilter(this String str)
        {
            if (string.IsNullOrEmpty(str))
                return str;
            str = str.Replace("<", "&#60;");
            str = str.Replace(">", "&#62;");
            str = str.Replace("'", "&prime;");
            str = str.Replace("=", "&#61;");
            str = str.Replace(";", "&#59;");
            str = str.Replace("|", "&#108;");
            str = Regex.Replace(str, "<script", "[script", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "</script", "[/script", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "drop", "&#100;&#114;&#111;&#112;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "delete", "&#100;&#101;&#108;&#101&#116;&#101;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "insert", "&#105;&#110;&#115;&#101;&#114;&#116;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "update", "&#117;&#112;&#100;&#97;&#116;&#101;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "\"", "&Prime;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "and", "&#97;&#110;&#100;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "or", "&#111;&#114;", RegexOptions.IgnoreCase);
            str = Regex.Replace(str, "not", "&#110;&#111;&#116;", RegexOptions.IgnoreCase);
            return str;
        }

        public static int IsNumeric(this String str)
        {
            int i;
            if (str != null && Regex.IsMatch(str, @"^-?\d+$"))
                i = int.Parse(str);
            else
                i = -1;
            return i;
        } 
    }
}
